package algorithm.kmp;

import java.util.Arrays;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-06-07 16:09
 * @ Description :
 */
public class KMPAlgo
{
    public static void main(String[] args)
    {
        String str1 = "BBC ABCDAB ABCDABCDABDE";
        String str2 = "ABCDABD";

        int[] next = kmpNext("ABCDABD");
        System.out.println(Arrays.toString(next));

        int index = kmpSearch(str1,str2,next);
        System.out.println("index = " + index);

        System.out.println(str1.indexOf(str2));

    }


    public static int kmpSearch(String str1, String str2, int[] next)
    {
        //i: str1的指针; j: str2的指针
        for (int i = 0, j = 0; i < str1.length(); i++)
        {

            //不相等时
            while (j > 0 && str1.charAt(i) != str2.charAt(j))
            {
                j = next[j - 1];
            }

            if (str1.charAt(i) == str2.charAt(j))
            {
                j++;
            }
            if (j == str2.length())
            {
                return i - j + 1;
            }
        }
        return -1;
    }

    //获取一个字符串的部分匹配值
    public static int[] kmpNext(String dest)
    {
        int[] next = new int[dest.length()];
        next[0] = 0;
        for (int i = 1, j = 0; i < dest.length(); i++)
        {
            while (j > 0 && dest.charAt(i) != dest.charAt(j))
            {
                j = next[j - 1];
            }
            if (dest.charAt(i) == dest.charAt(j))
            {
                j++;
            }
            next[i] = j;
        }
        return next;
    }


}
